package es.uji.viselab.benchmark.database;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.swing.JPanel;

import es.uji.viselab.util.LogLevel;
import es.uji.viselab.util.LogRecorder;
import es.uji.viselab.benchmark.DataPackage;

public class DBNonPersistent extends DBConnection {

	List<DataPackage> list = null;
	private static int sequence;

	// it is protected to defeat instantiation.
	public DBNonPersistent() {
		sequence = 0;
		list = new ArrayList<DataPackage>();
	}

	
	@Override
	public void insert(DataPackage dp) {
		sequence = sequence + 1;
		LogRecorder.log(LogLevel.STATS,
				"New record in database, seq: " + sequence + ":" + dp.print());
		list.add(dp);
	}

	@Override
	public void Connect() {
	}

	@Override
	public double[][] getData(BMDataType dt) {
		Iterator<DataPackage> iterator = list.iterator();
		int elements = 0;
		while (iterator.hasNext()) {
			DataPackage dp = iterator.next();
			if (dp.equalsType(dt))
				elements++;
		}
		double[][] result = new double[elements][dt.getNumElements()];
		int index = 0;
		int numValues = dt.getNumElements();
		iterator = list.iterator();
		while (iterator.hasNext()) {
			DataPackage dp = iterator.next();
			if (dp.equalsType(dt)) {
				for (int i = 0; i < numValues; i++)
					result[index][i] = dp.getValue(i+1);
				index++;
			}
		}
	//	iterator = list.iterator();
		return result;
	}

	@Override
	public JPanel getPanel() {
		return null;
	}

	@Override
	public int setNewExperiment() {
		return 0;
	}

	@Override
	public String getPanelTitle() {
		return null;
	}

	@Override
	public int panelWidth() {
		return 0;
	}

	@Override
	public int panelHeight() {
		return 0;
	}

}
